package com.github.ecbp.store.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.ecbp.common.data.service.impl.BaseServiceImpl;
import com.github.ecbp.common.data.utils.PagingUtil;
import com.github.ecbp.common.data.utils.TransformUtils;
import com.github.ecbp.common.utils.Asserts;
import com.github.ecbp.store.service.common.StoreCodeAndMessageEnum;
import com.github.ecbp.store.service.dto.*;
import com.github.ecbp.store.service.entity.*;
import com.github.ecbp.store.service.mapper.*;
import com.github.ecbp.store.service.service.ProductService;
import com.github.ecbp.store.service.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author Created by Alex·He on 2020/4/15 17:20
 * @Version 1.0
 * @Description 商品表(Product)表服务实现类
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<ProductMapper, Product> implements ProductService {
    @Autowired
    private ProductSkuMapper skuMapper;
    @Autowired
    private ProductPictureMapper pictureMapper;
    @Autowired
    private ProductSpecificationMapper specificationMapper;
    @Autowired
    private ProductSpecificationRelationMapper specificationRelationMapper;
    @Autowired
    private ProductParameterMapper parameterMapper;
    @Autowired
    private ProductParameterRelationMapper parameterRelationMapper;
    @Autowired
    private ProductFullReductionMapper fullReductionMapper;
    @Autowired
    private MemberPriceMapper memberPriceMapper;
    @Autowired
    private ProductVertifyRecordMapper vertifyRecordMapper;
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private ProductCategoryMapper categoryMapper;
    @Autowired
    private FreightTemplateMapper freightTemplateMapper;
    @Autowired
    private ProductAttributeCategoryRelationMapper attributeCategoryRelationMapper;
    @Autowired
    private ProductAttributeMapper attributeMapper;

    /**
     *功能描述:
     * @auther: Created by Alex·He 2020/4/17 23:33
     * @description: 保存商品信息
     */
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public boolean save(ProductDTO param) {
        Product product = TransformUtils.copy(param, Product.class);
        Asserts.checkNotNull(product, StoreCodeAndMessageEnum.PRODUCT_NOT_EXISTS_ERROR);
        // 保存商品信息
        Asserts.checkHandle(baseMapper.insert(product), StoreCodeAndMessageEnum.PRODUCT_SAVE_ERROR);
        Long id = product.getId();
        ProductSkuDTO skuDTO = param.getSkuDTO();
        if (!ObjectUtils.isEmpty(skuDTO)) {
            ProductSku sku = TransformUtils.copy(skuDTO, ProductSku.class);
            sku.setProductId(id);
            // 保存商品SKU
            Asserts.checkHandle(skuMapper.insert(sku), StoreCodeAndMessageEnum.PRODUCT_SKU_SAVE_ERROR);
        }
        ProductPictureDTO pictureDTO = param.getPictureDTO();
        if (!ObjectUtils.isEmpty(pictureDTO)) {
            ProductPicture picture = TransformUtils.copy(pictureDTO, ProductPicture.class);
            picture.setProductId(id);
            // 保存商品图片
            Asserts.checkHandle(pictureMapper.insert(picture), StoreCodeAndMessageEnum.PRODUCT_PICTURE_SAVE_ERROR);
        }
        ProductSpecificationDTO specificationDTO = param.getSpecificationDTO();
        if (!ObjectUtils.isEmpty(specificationDTO)) {
            ProductSpecification specification = TransformUtils.copy(specificationDTO, ProductSpecification.class);
            // 保存商品规格
            Asserts.checkHandle(specificationMapper.insert(specification), StoreCodeAndMessageEnum.PRODUCT_SPEC_SAVE_ERROR);
            // 建立商品与规格的关系
            insertProductSpecificationRelation(id, specification.getId());
        }
        ProductParameterDTO parameterDTO = param.getParameterDTO();
        if (!ObjectUtils.isEmpty(parameterDTO)) {
            ProductParameter parameter = TransformUtils.copy(parameterDTO, ProductParameter.class);
            // 保存商品参数
            Asserts.checkHandle(parameterMapper.insert(parameter), StoreCodeAndMessageEnum.PRODUCT_PARAM_SAVE_ERROR);
            // 建立商品与参数的关系
            insertProductParameterRelation(id, parameter.getId());
        }
        ProductFullReductionDTO fullReductionDTO = param.getFullReductionDTO();
        if (!ObjectUtils.isEmpty(fullReductionDTO)) {
            ProductFullReduction fullReduction = TransformUtils.copy(fullReductionDTO, ProductFullReduction.class);
            fullReduction.setProductId(id);
            // 保存商品满减信息
            Asserts.checkHandle(fullReductionMapper.insert(fullReduction), StoreCodeAndMessageEnum.PRODUCT_FULL_REDUCTION_SAVE_ERROR);
        }
        MemberPriceDTO memberPriceDTO = param.getMemberPriceDTO();
        if (!ObjectUtils.isEmpty(memberPriceDTO)) {
            MemberPrice memberPrice = TransformUtils.copy(memberPriceDTO, MemberPrice.class);
            memberPrice.setProductId(id);
            // 保存会员价格信息
            Asserts.checkHandle(memberPriceMapper.insert(memberPrice), StoreCodeAndMessageEnum.PRODUCT_MEMBER_PRICE_SAVE_ERROR);
        }
        ProductVertifyRecordDTO vertifyRecordDTO = param.getVertifyRecordDTO();
        if (!ObjectUtils.isEmpty(vertifyRecordDTO)) {
            ProductVertifyRecord vertifyRecord = TransformUtils.copy(vertifyRecordDTO, ProductVertifyRecord.class);
            vertifyRecord.setProductId(id);
            // 保存商品审核状态
            Asserts.checkHandle(vertifyRecordMapper.insert(vertifyRecord), StoreCodeAndMessageEnum.VERTIFY_RECORD_SAVE_ERROR);
        }
        return true;
    }

    /**
     *功能描述:
     * @auther: Created by Alex·He 2020/5/1 8:52
     * @description: 根据ID删除商品，包括删除与商品相关的关系
     */
    @Override
    public boolean removeById(Long id) {
        // 删除商品SKU
        deleteProductSku(id);
        // 删除商品图片
        deleteProductPicture(id);
        // 删除商品与规格的关系
        deleteProductSpecificationRelation(id);
        // 删除商品与参数的关系
        deleteProductParameterRelation(id);
        // 删除商品满减信息
        deleteFullReduction(id);
        // 删除会员信息
        deleteMemberPrice(id);
        // TODO 待议
        // 删除商品审核记录
//        deleteVertifyRecord(id);
        // 删除商品信息
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>(Product.class);
        wrapper.eq(Product::getId, id);
        Asserts.checkHandle(baseMapper.deleteWithFill(new Product(), wrapper), StoreCodeAndMessageEnum.PRODUCT_DELETE_ERROR);
        return true;
    }

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/5/4 9:22
     * @version 1.0
     * @description 批量删除商品
     */
    @Override
    public boolean removeByIds(List<Long> ids) {
        // TODO 需优化批量删除
        for (Long id : ids) {
            removeById(id);
        }
        return true;
    }

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/5/4 9:27
     * @version 1.0
     * @description 根据ID修改商品
     */
    @Override
    public boolean updateById(ProductDTO param) {
        Product product = TransformUtils.copy(param, Product.class);
        Asserts.checkNotNull(product, StoreCodeAndMessageEnum.PRODUCT_NOT_EXISTS_ERROR);
        // 保存商品信息
        Asserts.checkHandle(baseMapper.updateById(product), StoreCodeAndMessageEnum.PRODUCT_SAVE_ERROR);
        Long id = product.getId();
        // 更新商品SKU
        ProductSkuDTO skuDTO = param.getSkuDTO();
        if (!ObjectUtils.isEmpty(skuDTO)) {
            ProductSku sku = TransformUtils.copy(skuDTO, ProductSku.class);
            sku.setProductId(id);
            Asserts.checkHandle(skuMapper.updateById(sku), StoreCodeAndMessageEnum.PRODUCT_SKU_SAVE_ERROR);
        }
        // 更新商品图片
        ProductPictureDTO pictureDTO = param.getPictureDTO();
        if (!ObjectUtils.isEmpty(pictureDTO)) {
            ProductPicture picture = TransformUtils.copy(pictureDTO, ProductPicture.class);
            picture.setProductId(id);
            Asserts.checkHandle(pictureMapper.updateById(picture), StoreCodeAndMessageEnum.PRODUCT_PICTURE_SAVE_ERROR);
        }
        ProductSpecificationDTO specificationDTO = param.getSpecificationDTO();
        if (!ObjectUtils.isEmpty(specificationDTO)) {
            ProductSpecification specification = TransformUtils.copy(specificationDTO, ProductSpecification.class);
            // 保存商品规格
            Asserts.checkHandle(specificationMapper.updateById(specification), StoreCodeAndMessageEnum.PRODUCT_SPEC_SAVE_ERROR);
            // 删除商品与规格的原关系
            deleteProductSpecificationRelation(id);
            // 重新建立商品与规格的关系
            insertProductSpecificationRelation(id, specification.getId());
        }
        ProductParameterDTO parameterDTO = param.getParameterDTO();
        if (!ObjectUtils.isEmpty(parameterDTO)) {
            ProductParameter parameter = TransformUtils.copy(parameterDTO, ProductParameter.class);
            // 更新商品参数
            Asserts.checkHandle(parameterMapper.updateById(parameter), StoreCodeAndMessageEnum.PRODUCT_PARAM_SAVE_ERROR);
            // 删除商品与参数的原关系
            deleteProductParameterRelation(id);
            // 重新建立商品与参数的关系
            insertProductParameterRelation(id, parameter.getId());
        }
        ProductFullReductionDTO fullReductionDTO = param.getFullReductionDTO();
        if (!ObjectUtils.isEmpty(fullReductionDTO)) {
            ProductFullReduction fullReduction = TransformUtils.copy(fullReductionDTO, ProductFullReduction.class);
            fullReduction.setProductId(id);
            // 更新商品满减信息
            Asserts.checkHandle(fullReductionMapper.updateById(fullReduction), StoreCodeAndMessageEnum.PRODUCT_FULL_REDUCTION_SAVE_ERROR);
        }
        MemberPriceDTO memberPriceDTO = param.getMemberPriceDTO();
        if (!ObjectUtils.isEmpty(memberPriceDTO)) {
            MemberPrice memberPrice = TransformUtils.copy(memberPriceDTO, MemberPrice.class);
            memberPrice.setProductId(id);
            // 更新会员价格信息
            Asserts.checkHandle(memberPriceMapper.updateById(memberPrice), StoreCodeAndMessageEnum.PRODUCT_MEMBER_PRICE_SAVE_ERROR);
        }
        return true;
    }

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/5/4 9:53
     * @version 1.0
     * @description 修改商品的上架状态
     */
    @Override
    public boolean updatePublishStatus(Long id, Integer publishStatus) {
        Product product = new Product();
        product.setId(id);
        product.setPublishStatus(publishStatus);
        return super.updateByIdBase(product);
    }

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/5/4 9:58
     * @version 1.0
     * @description 修改商品的新品状态
     */
    @Override
    public boolean updateNewStatus(Long id, Integer newStatus) {
        Product product = new Product();
        product.setId(id);
        product.setNewStatus(newStatus);
        return super.updateByIdBase(product);
    }

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/5/4 10:02
     * @version 1.0
     * @description 修改商品的人气推荐状态
     */
    @Override
    public boolean updateRecommandStatus(Long id, Integer recommandStatus) {
        Product product = new Product();
        product.setId(id);
        product.setRecommandStatus(recommandStatus);
        return super.updateByIdBase(product);
    }

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/5/4 10:05
     * @version 1.0
     * @description 修改商品的预告商品状态
     */
    @Override
    public boolean updatePreviewStatus(Long id, Integer previewStatus) {
        Product product = new Product();
        product.setId(id);
        product.setPreviewStatus(previewStatus);
        return super.updateByIdBase(product);
    }

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/5/4 10:28
     * @version 1.0
     * @description 根据ID获取商品详情
     */
    @Override
    public ProductVO getById(Long id) {
        // 获取商品信息
        ProductVO productVO = TransformUtils.copy(super.getById(id), ProductVO.class);
        // 设置品牌的名称和Logo
        productVO.setBrandVO(getBrandWithNameAndLogo(productVO.getBrandId()));
        // 设置商品分类的名称和图标
        Long categoryId = productVO.getProductCategoryId();
        productVO.setProductCategoryVO(getCategoryWithNameAndIcon(categoryId));
        // 设置运费模板
        productVO.setFreightTemplateVO(TransformUtils.copy(freightTemplateMapper.selectById(productVO.getFreightTemplateId()),
                FreightTemplateVO.class));
        // 设置商品SKU信息
        productVO.setProductSkuVO(getProductSku(id));
        // 设置商品图片
        productVO.setProductPictureVO(getProductPicture(id));
        // 设置商品属性列表
        productVO.setProductAttributeVOList(getProductAttributeList(categoryId));
        // 设置商品规格列表
        productVO.setProductSpecificationVOList(getProductSpecificationList(id));
        // 设置商品参数列表
        productVO.setProductParameterVOList(getProductParameterList(id));
        // 设置会员价格
        productVO.setMemberPriceVO(getMemberPrice(id));
        // 设置商品满减价格
        productVO.setProductFullReductionVO(getProductFullReduction(id));
        // 设置商品审核信息
        productVO.setVertifyRecordVO(getVertifyRecord(id));
        return productVO;
    }

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/5/4 10:37
     * @version 1.0
     * @description 分页获取商品信息
     */
    @Override
    public IPage<ProductPageVO> page(ProductQueryDTO param) {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        assemble(TransformUtils.copy(param, Product.class), wrapper, param);
        IPage<ProductPageVO> objectPageVo = new Page<>(PagingUtil.getPage(), PagingUtil.getLimit());
        return objectPageVo.setRecords(baseMapper.selectPageWithAll(objectPageVo, wrapper));
    }

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/5/4 9:40
     * @version 1.0
     * @description 建立商品与规格的关系
     */
    private void insertProductSpecificationRelation(Long productId, Long productSpecificationId) {
        ProductSpecificationRelation relation = new ProductSpecificationRelation();
        relation.setProductId(productId);
        relation.setProductSpecificationId(productSpecificationId);
        Asserts.checkHandle(specificationRelationMapper.insert(relation), StoreCodeAndMessageEnum.PRODUCT_SPEC_REL_SAVE_ERROR);
    }

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/5/4 9:43
     * @version 1.0
     * @description 建立商品与参数的关系
     */
    private void insertProductParameterRelation(Long productId, Long productParameterId) {
        ProductParameterRelation relation = new ProductParameterRelation();
        relation.setProductId(productId);
        relation.setProductParameterId(productParameterId);
        Asserts.checkHandle(parameterRelationMapper.insert(relation), StoreCodeAndMessageEnum.PRODUCT_PARAM_REL_SAVE_ERROR);
    }

    /**
     *功能描述:
     * @auther: Created by Alex·He 2020/5/1 9:06
     * @description: 删除商品SKU
     */
    private void deleteProductSku(Long id) {
        LambdaQueryWrapper<ProductSku> wrapper = new LambdaQueryWrapper<>(ProductSku.class);
        wrapper.eq(ProductSku::getProductId, id);
        Asserts.checkHandle(skuMapper.deleteWithFill(new ProductSku(), wrapper),
                StoreCodeAndMessageEnum.PRODUCT_SKU_DELETE_ERROR);
    }

    /**
     *功能描述:
     * @auther: Created by Alex·He 2020/5/1 9:11
     * @description: 删除商品图片
     */
    private void deleteProductPicture(Long id) {
        LambdaQueryWrapper<ProductPicture> wrapper = new LambdaQueryWrapper<>(ProductPicture.class);
        wrapper.eq(ProductPicture::getProductId, id);
        Asserts.checkHandle(pictureMapper.deleteWithFill(new ProductPicture(), wrapper),
                StoreCodeAndMessageEnum.PRODUCT_PICTURE_DELETE_ERROR);
    }

    /**
     *功能描述:
     * @auther: Created by Alex·He 2020/5/1 9:15
     * @description: 删除商品与规格的关系
     */
    private void deleteProductSpecificationRelation(Long id) {
        LambdaQueryWrapper<ProductSpecificationRelation> wrapper = new LambdaQueryWrapper<>(ProductSpecificationRelation.class);
        wrapper.eq(ProductSpecificationRelation::getProductId, id);
        Asserts.checkHandle(specificationRelationMapper.deleteWithFill(new ProductSpecificationRelation(), wrapper),
                StoreCodeAndMessageEnum.PRODUCT_SPEC_REL_DELETE_ERROR);
    }

    /**
     *功能描述:
     * @auther: Created by Alex·He 2020/5/1 9:17
     * @description: 删除商品与参数的关系
     */
    private void deleteProductParameterRelation(Long id) {
        LambdaQueryWrapper<ProductParameterRelation> wrapper = new LambdaQueryWrapper<>(ProductParameterRelation.class);
        wrapper.eq(ProductParameterRelation::getProductId, id);
        Asserts.checkHandle(parameterRelationMapper.deleteWithFill(new ProductParameterRelation(), wrapper),
                StoreCodeAndMessageEnum.PRODUCT_PARAM_REL_DELETE_ERROR);
    }

    /**
     *功能描述:
     * @auther: Created by Alex·He 2020/5/1 9:20
     * @description: 删除商品满减信息
     */
    private void deleteFullReduction(Long id) {
        LambdaQueryWrapper<ProductFullReduction> wrapper = new LambdaQueryWrapper<>(ProductFullReduction.class);
        wrapper.eq(ProductFullReduction::getProductId, id);
        Asserts.checkHandle(fullReductionMapper.deleteWithFill(new ProductFullReduction(), wrapper),
                StoreCodeAndMessageEnum.PRODUCT_FULL_REDUCTION_DELETE_ERROR);
    }

    /**
     *功能描述:
     * @auther: Created by Alex·He 2020/5/1 9:22
     * @description: 删除会员信息
     */
    private void deleteMemberPrice(Long id) {
        LambdaQueryWrapper<MemberPrice> wrapper = new LambdaQueryWrapper<>(MemberPrice.class);
        wrapper.eq(MemberPrice::getProductId, id);
        Asserts.checkHandle(memberPriceMapper.deleteWithFill(new MemberPrice(), wrapper),
                StoreCodeAndMessageEnum.PRODUCT_MEMBER_PRICE_DELETE_ERROR);
    }

    /**
     *功能描述:
     * @auther: Created by Alex·He 2020/5/6 21:47
     * @description: 删除商品审核记录
     */
    private void deleteVertifyRecord(Long id) {
        LambdaQueryWrapper<ProductVertifyRecord> wrapper = new LambdaQueryWrapper<>(ProductVertifyRecord.class);
        wrapper.eq(ProductVertifyRecord::getProductId, id);
        Asserts.checkHandle(vertifyRecordMapper.deleteWithFill(new ProductVertifyRecord(), wrapper),
                StoreCodeAndMessageEnum.VERTIFY_RECORD_DELETE_ERROR);
    }

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/5/6 11:19
     * @version 1.0
     * @description 获取品牌的名称和Logo
     */
    private BrandVO getBrandWithNameAndLogo(Long brandId) {
        LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>(Brand.class);
        wrapper.select(Brand::getName, Brand::getLogo);
        wrapper.in(Brand::getId, brandId);
        return TransformUtils.copy(brandMapper.selectOne(wrapper), BrandVO.class);
    }

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/5/6 11:24
     * @version 1.0
     * @description 获取商品分类的名称和图标
     */
    private ProductCategoryVO getCategoryWithNameAndIcon(Long productCategoryId) {
        LambdaQueryWrapper<ProductCategory> wrapper = new LambdaQueryWrapper<>(ProductCategory.class);
        wrapper.select(ProductCategory::getName, ProductCategory::getIcon);
        wrapper.in(ProductCategory::getId, productCategoryId);
        return TransformUtils.copy(categoryMapper.selectOne(wrapper), ProductCategoryVO.class);
    }

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/5/6 14:56
     * @version 1.0
     * @description 获取商品SKU信息
     */
    private ProductSkuVO getProductSku(Long productId) {
        LambdaQueryWrapper<ProductSku> wrapper = new LambdaQueryWrapper<>(ProductSku.class);
        wrapper.in(ProductSku::getProductId, productId);
        return TransformUtils.copy(skuMapper.selectOne(wrapper), ProductSkuVO.class);
    }

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/5/6 15:03
     * @version 1.0
     * @description 获取商品图片
     */
    private ProductPictureVO getProductPicture(Long productId) {
        LambdaQueryWrapper<ProductPicture> wrapper = new LambdaQueryWrapper<>(ProductPicture.class);
        wrapper.in(ProductPicture::getProductId, productId);
        return TransformUtils.copy(pictureMapper.selectOne(wrapper), ProductPictureVO.class);
    }

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/5/6 15:24
     * @version 1.0
     * @description 获取商品属性列表
     */
    private List<ProductAttributeVO> getProductAttributeList(Long productCategoryId) {
        // 获取商品属性与商品分类的关系
        LambdaQueryWrapper<ProductAttributeCategoryRelation> wrapper =
                new LambdaQueryWrapper<>(ProductAttributeCategoryRelation.class);
        wrapper.select(ProductAttributeCategoryRelation::getProductAttributeId);
        wrapper.in(ProductAttributeCategoryRelation::getProductCategoryId, productCategoryId);
        List<ProductAttributeCategoryRelation> relationList = attributeCategoryRelationMapper.selectList(wrapper);
        // 获取商品属性Id
        Set<Long> attrIds = relationList.stream().map(ProductAttributeCategoryRelation::getProductAttributeId)
                .collect(Collectors.toSet());
        // 获取商品属性
        LambdaQueryWrapper<ProductAttribute> attrWrapper = new LambdaQueryWrapper<>(ProductAttribute.class);
        attrWrapper.in(ProductAttribute::getId, attrIds);
        return TransformUtils.listCopy(attributeMapper.selectList(attrWrapper), ProductAttributeVO.class);
    }

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/5/6 15:43
     * @version 1.0
     * @description 获取商品规格列表
     */
    private List<ProductSpecificationVO> getProductSpecificationList(Long productId) {
        // 获取商品与规格的关系
        LambdaQueryWrapper<ProductSpecificationRelation> wrapper = new LambdaQueryWrapper<>(ProductSpecificationRelation.class);
        wrapper.select(ProductSpecificationRelation::getProductSpecificationId);
        wrapper.in(ProductSpecificationRelation::getProductId, productId);
        List<ProductSpecificationRelation> relationList = specificationRelationMapper.selectList(wrapper);
        // 获取商品规格Id
        Set<Long> specIds = relationList.stream().map(ProductSpecificationRelation::getProductSpecificationId)
                .collect(Collectors.toSet());
        // 获取商品规格
        LambdaQueryWrapper<ProductSpecification> specWrapper = new LambdaQueryWrapper<>(ProductSpecification.class);
        specWrapper.in(ProductSpecification::getId, specIds);
        return TransformUtils.listCopy(specificationMapper.selectList(specWrapper), ProductSpecificationVO.class);
    }

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/5/6 15:55
     * @version 1.0
     * @description 获取商品参数列表
     */
    private List<ProductParameterVO> getProductParameterList(Long productId) {
        // 获取商品与参数的关系
        LambdaQueryWrapper<ProductParameterRelation> wrapper = new LambdaQueryWrapper<>(ProductParameterRelation.class);
        wrapper.select(ProductParameterRelation::getProductParameterId);
        wrapper.in(ProductParameterRelation::getProductId, productId);
        List<ProductParameterRelation> relationList = parameterRelationMapper.selectList(wrapper);
        // 获取商品参数Id
        Set<Long> paramIds = relationList.stream().map(ProductParameterRelation::getProductParameterId).collect(Collectors.toSet());
        // 获取商品参数
        LambdaQueryWrapper<ProductParameter> paramWrapper = new LambdaQueryWrapper<>(ProductParameter.class);
        paramWrapper.in(ProductParameter::getId, paramIds);
        return TransformUtils.listCopy(parameterMapper.selectList(paramWrapper), ProductParameterVO.class);
    }

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/5/6 16:00
     * @version 1.0
     * @description 获取会员价格
     */
    private MemberPriceVO getMemberPrice(Long productId) {
        LambdaQueryWrapper<MemberPrice> wrapper = new LambdaQueryWrapper<>(MemberPrice.class);
        wrapper.in(MemberPrice::getProductId, productId);
        return TransformUtils.copy(memberPriceMapper.selectOne(wrapper), MemberPriceVO.class);
    }

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/5/6 16:01
     * @version 1.0
     * @description 获取商品满减价格
     */
    private ProductFullReductionVO getProductFullReduction(Long productId) {
        LambdaQueryWrapper<ProductFullReduction> wrapper = new LambdaQueryWrapper<>(ProductFullReduction.class);
        wrapper.in(ProductFullReduction::getProductId, productId);
        return TransformUtils.copy(fullReductionMapper.selectOne(wrapper), ProductFullReductionVO.class);
    }

    /**
     *功能描述:
     * @auther: Created by Alex·He 2020/5/6 22:21
     * @description: 获取审核信息
     */
    private ProductVertifyRecordVO getVertifyRecord(Long productId) {
        LambdaQueryWrapper<ProductVertifyRecord> wrapper = new LambdaQueryWrapper<>(ProductVertifyRecord.class);
        wrapper.in(ProductVertifyRecord::getProductId, productId);
        return TransformUtils.copy(vertifyRecordMapper.selectOne(wrapper), ProductVertifyRecordVO.class);
    }

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/5/6 17:20
     * @version 1.0
     * @description 模糊查询的参数组合，可通过商品名称查询
     */
    private void assemble(Product entity, QueryWrapper<Product> wrapper, ProductQueryDTO param) {
        if (null != entity.getId()) {
            wrapper.eq("p.id", entity.getId());
        }
        wrapper.eq("p.delete_status", 1);
        wrapper.eq("sku.delete_status", 1);
        if (!StringUtils.isEmpty(param.getName())) {
            wrapper.likeRight("p.name", param.getName());
        }
        if (!StringUtils.isEmpty(param.getPublishStatus())) {
            wrapper.likeRight("p.publish_status", param.getPublishStatus());
        }
        if (!StringUtils.isEmpty(param.getNewStatus())) {
            wrapper.likeRight("p.new_status", param.getNewStatus());
        }
        if (!StringUtils.isEmpty(param.getRecommandStatus())) {
            wrapper.likeRight("p.recommand_status", param.getRecommandStatus());
        }
        if (!StringUtils.isEmpty(param.getPreviewStatus())) {
            wrapper.likeRight("p.preview_status", param.getPreviewStatus());
        }
        if (!StringUtils.isEmpty(param.getProductSn())) {
            wrapper.likeRight("sku.product_sn", param.getProductSn());
        }
        if (!StringUtils.isEmpty(param.getVerifyStatus())) {
            wrapper.likeRight("pvr.verifyStatus", param.getVerifyStatus());
        }
        wrapper.orderByDesc("p.create_time");
    }
}

